1 //---- input parameters ---------------------------------------------+
\r
3 extern int INCREMENT=35;
\r
4 extern double LOTS=0.1;
\r
5 extern int LEVELS=3;
\r
6 extern double MAX_LOTS=99;
\r
7 extern int MAGIC=1803;
\r
8 extern bool CONTINUE=true;
\r
9 extern bool MONEY_MANAGEMENT=false;
\r
10 extern int RISK_RATIO=2;
\r
11 //+------------------------------------------------------------------+
\r
13 bool UseProfitTarget=false;
\r
14 bool UsePartialProfitTarget=false;
\r
15 int Target_Increment = 50;
\r
16 int First_Target = 20;
\r
17 bool UseEntryTime=false;
\r
20 //+------------------------------------------------------------------+
\r
27 //+------------------------------------------------------------------+
\r
28 nextTP = First_Target;
\r
29 //+------------------------------------------------------------------+
\r
32 //+------------------------------------------------------------------+
\r
33 //| expert deinitialization function |
\r
34 //+------------------------------------------------------------------+
\r
39 //+------------------------------------------------------------------+
\r
40 //| expert start function |
\r
41 //+------------------------------------------------------------------+
\r
44 int ticket, cpt, profit, total=0, BuyGoalProfit, SellGoalProfit, PipsLot;
\r
45 double ProfitTarget=INCREMENT*2, BuyGoal=0, SellGoal=0, spread=(Ask-Bid)/Point, InitialPrice=0;
\r
48 if(INCREMENT<MarketInfo(Symbol(),MODE_STOPLEVEL)+spread) INCREMENT=1+MarketInfo(Symbol(),MODE_STOPLEVEL)+spread;
\r
49 if(MONEY_MANAGEMENT) LOTS=NormalizeDouble(AccountBalance()*AccountLeverage()/1000000*RISK_RATIO,0)*MarketInfo(Symbol(),MODE_MINLOT);
\r
50 if(LOTS<MarketInfo(Symbol(),MODE_MINLOT))
\r
52 Comment("Not Enough Free Margin to begin");
\r
55 for(cpt=1;cpt<LEVELS;cpt++) PipsLot+=cpt*INCREMENT;
\r
56 for(cpt=0;cpt<OrdersTotal();cpt++)
\r
58 OrderSelect(cpt,SELECT_BY_POS,MODE_TRADES);
\r
59 if(OrderMagicNumber()==MAGIC && OrderSymbol()==Symbol())
\r
62 if(!InitialPrice) InitialPrice=StrToDouble(OrderComment());
\r
63 if(UsePartialProfitTarget && UseProfitTarget && OrderType()<2)
\r
65 double val=getPipValue(OrderOpenPrice(),OrderType());
\r
66 takeProfit(val,OrderTicket());
\r
70 if(total<1 && Enter && (!UseEntryTime || (UseEntryTime && Hour()==EntryTime)))
\r
72 if(AccountFreeMargin()<(100*LOTS))
\r
74 Print("Not enough free margin to begin");
\r
77 // - Open Check - Start Cycle
\r
79 SellGoal=InitialPrice-(LEVELS+1)*INCREMENT*Point;
\r
80 BuyGoal=InitialPrice+(LEVELS+1)*INCREMENT*Point;
\r
81 for(cpt=1;cpt<=LEVELS;cpt++)
\r
83 OrderSend(Symbol(),OP_BUYSTOP,LOTS,InitialPrice+cpt*INCREMENT*Point,2,SellGoal,BuyGoal,DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),MAGIC,0);
\r
84 OrderSend(Symbol(),OP_SELLSTOP,LOTS,InitialPrice-cpt*INCREMENT*Point,2,BuyGoal+spread*Point,SellGoal+spread*Point,DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),MAGIC,0);
\r
86 } // initial setup done - all channels are set up
\r
87 else // We have open Orders
\r
89 BuyGoal=InitialPrice+INCREMENT*(LEVELS+1)*Point;
\r
90 SellGoal=InitialPrice-INCREMENT*(LEVELS+1)*Point;
\r
91 total=OrdersHistoryTotal();
\r
92 for(cpt=0;cpt<total;cpt++)
\r
94 OrderSelect(cpt,SELECT_BY_POS,MODE_HISTORY);
\r
95 if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC && StrToDouble(OrderComment())==InitialPrice){EndSession();return(0);}
\r
97 if(UseProfitTarget && CheckProfits(LOTS,OP_SELL,true,InitialPrice)>ProfitTarget) {EndSession();return(0);}
\r
98 BuyGoalProfit=CheckProfits(LOTS,OP_BUY,false,InitialPrice);
\r
99 SellGoalProfit=CheckProfits(LOTS,OP_SELL,false,InitialPrice);
\r
100 if(BuyGoalProfit<ProfitTarget)
\r
101 // - Incriment Lots Buy
\r
103 for(cpt=LEVELS;cpt>=1 && BuyGoalProfit<ProfitTarget;cpt--)
\r
105 if(Ask<=(InitialPrice+(cpt*INCREMENT-MarketInfo(Symbol(),MODE_STOPLEVEL))*Point))
\r
107 ticket=OrderSend(Symbol(),OP_BUYSTOP,cpt*LOTS,InitialPrice+cpt*INCREMENT*Point,2,SellGoal,BuyGoal,DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),MAGIC,0);
\r
109 if(ticket>0) BuyGoalProfit+=LOTS*(BuyGoal-InitialPrice-cpt*INCREMENT*Point)/Point;
\r
112 if(SellGoalProfit<ProfitTarget)
\r
113 // - Increment Lots Sell
\r
115 for(cpt=LEVELS;cpt>=1 && SellGoalProfit<ProfitTarget;cpt--)
\r
117 if(Bid>=(InitialPrice-(cpt*INCREMENT-MarketInfo(Symbol(),MODE_STOPLEVEL))*Point))
\r
119 ticket=OrderSend(Symbol(),OP_SELLSTOP,cpt*LOTS,InitialPrice-cpt*INCREMENT*Point,2,BuyGoal+spread*Point,SellGoal+spread*Point,DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),MAGIC,0);
\r
121 if(ticket>0) SellGoalProfit+=LOTS*(InitialPrice-cpt*INCREMENT*Point-SellGoal-spread*Point)/Point;
\r
125 //+------------------------------------------------------------------+
\r
127 Comment("mGRID EXPERT ADVISOR ver 2.0\n",
\r
128 "FX Acc Server:",AccountServer(),"\n",
\r
129 "Date: ",Month(),"-",Day(),"-",Year()," Server Time: ",Hour(),":",Minute(),":",Seconds(),"\n",
\r
130 "Minimum Lot Sizing: ",MarketInfo(Symbol(),MODE_MINLOT),"\n",
\r
131 "Account Balance: $",AccountBalance(),"\n",
\r
132 "Symbol: ", Symbol(),"\n",
\r
133 "Price: ",NormalizeDouble(Bid,4),"\n",
\r
134 "Pip Spread: ",MarketInfo("EURUSD",MODE_SPREAD),"\n",
\r
135 "Increment=" + INCREMENT,"\n",
\r
136 "Lots: ",LOTS,"\n",
\r
137 "Levels: " + LEVELS,"\n");
\r
141 //+------------------------------------------------------------------+
\r
143 int CheckProfits(double LOTS, int Goal, bool Current, double InitialPrice)
\r
146 if(Current)//return current profit
\r
148 for(cpt=0;cpt<OrdersTotal();cpt++)
\r
150 OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);
\r
151 if(OrderSymbol()==Symbol() && StrToDouble(OrderComment())==InitialPrice)
\r
153 if(OrderType()==OP_BUY) profit+=(Bid-OrderOpenPrice())/Point*OrderLots()/LOTS;
\r
154 if(OrderType()==OP_SELL) profit+=(OrderOpenPrice()-Ask)/Point*OrderLots()/LOTS;
\r
163 for(cpt=0;cpt<OrdersTotal();cpt++)
\r
165 OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);
\r
166 if(OrderSymbol()==Symbol() && StrToDouble(OrderComment())==InitialPrice)
\r
168 if(OrderType()==OP_BUY) profit+=(OrderTakeProfit()-OrderOpenPrice())/Point*OrderLots()/LOTS;
\r
169 if(OrderType()==OP_SELL) profit-=(OrderStopLoss()-OrderOpenPrice())/Point*OrderLots()/LOTS;
\r
170 if(OrderType()==OP_BUYSTOP) profit+=(OrderTakeProfit()-OrderOpenPrice())/Point*OrderLots()/LOTS;
\r
177 for(cpt=0;cpt<OrdersTotal();cpt++)
\r
179 OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);
\r
180 if(OrderSymbol()==Symbol() && StrToDouble(OrderComment())==InitialPrice)
\r
182 if(OrderType()==OP_BUY) profit-=(OrderOpenPrice()-OrderStopLoss())/Point*OrderLots()/LOTS;
\r
183 if(OrderType()==OP_SELL) profit+=(OrderOpenPrice()-OrderTakeProfit())/Point*OrderLots()/LOTS;
\r
184 if(OrderType()==OP_SELLSTOP) profit+=(OrderOpenPrice()-OrderTakeProfit())/Point*OrderLots()/LOTS;
\r
194 int cpt, total=OrdersTotal();
\r
195 for(cpt=0;cpt<total;cpt++)
\r
198 OrderSelect(cpt,SELECT_BY_POS,MODE_TRADES);
\r
199 if(OrderSymbol()==Symbol() && OrderType()>1) OrderDelete(OrderTicket());
\r
200 else if(OrderSymbol()==Symbol() && OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,3);
\r
201 else if(OrderSymbol()==Symbol() && OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,3);
\r
204 if(!CONTINUE) Enter=false;
\r
209 double getPipValue(double ord,int dir)
\r
213 if(dir == 1) val=(NormalizeDouble(ord,Digits) - NormalizeDouble(Ask,Digits));
\r
214 else val=(NormalizeDouble(Bid,Digits) - NormalizeDouble(ord,Digits));
\r
219 //========== FUNCTION takeProfit
\r
221 void takeProfit(int current_pips, int ticket)
\r
223 if(OrderSelect(ticket, SELECT_BY_TICKET))
\r
226 if(current_pips >= nextTP && current_pips < (nextTP + Target_Increment))
\r
230 if(OrderClose(ticket, MAX_LOTS, Ask, 3))
\r
231 nextTP+=Target_Increment;
\r
233 Print("Error closing order : ",GetLastError());
\r
237 if(OrderClose(ticket, MAX_LOTS, Bid, 3))
\r
238 nextTP+=Target_Increment;
\r
240 Print("Error closing order : ",GetLastError());
\r